"""
Case Type   : 服务端工具
Case Name   : 验证gs_checkperf工具功能
Create At   : 2024.10.8
Owner       : wang-tianjing1
Description :
    1.显示帮助信息
    2.显示版本号信息
    3.opengauss安装安装用户检查openGauss性能
Expect      :
    1.显示正确
    2.显示正确
    3.显示正确
History     :
"""
import unittest
import os
from yat.test import Node
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger



class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_node = Node('PrimaryDbUser')
        self.Constant = Constant()

    def test_server_tools1(self):
        self.log.info('--------step1:显示帮助信息;except:显示正确------------------')
        cmd_list = ['-?', '--help']
        for cmd in cmd_list:
            check_cmd1 = f'''source {macro.DB_ENV_PATH}
                gs_checkperf {cmd}'''
            self.log.info(check_cmd1)
            msg1 = self.primary_node.sh(check_cmd1).result()
            self.log.info(msg1)
            self.log.info('---------------从Usage:开始截取---------------')
            start_index1 = msg1.find('Usage:')+len('Usage:')
            start_index2 = msg1.find('General options:')
            self.log.info('---------------截取语法部分---------------')
            temp = msg1[start_index1:start_index2].split('\n')
            self.log.info(temp)
            options_list1 = []
            for i in temp[1:-2]:
                options_list1.append(i.strip())
            self.log.info(options_list1)
            grammar = [
                'gs_checkperf -? | --help',
                'gs_checkperf -V | --version',
                'gs_checkperf [-U USER] [-o OUTPUT] [-i ITEM] [--detail] [-l LOGFILE]']
            if len(options_list1) == len(grammar):
                for opt in options_list1:
                    if opt in grammar:
                        self.log.info(f'{opt}---------语法校验通过---------')
                    else:
                        self.log.error(f'{opt}---------语法校验不通过---------')
            else:
                self.log.error('---------语法校验有误---------')

            self.log.info('---------------截取参数部分---------------')
            options_list2 = []
            for i in msg1[start_index2:].split('\n'):
                for j in i.split(' '):
                    if len(j) != 0:
                        if j[0] == '-':
                            options_list2.append(j)
                        else:
                            pass
                    else:
                        pass
            self.log.info(options_list2)
            parameter = ['-U', '-o', '-i', '-i', '-i', '--detail', '-l', '-?,', '--help', '-V,', '--version']
            if len(options_list2) == len(parameter):
                for opt in options_list2:
                    if opt in parameter:
                        self.log.info(f'{opt}---------参数校验通过---------')
                    else:
                        self.log.error(f'{opt}---------参数校验不通过---------')
            else:
                self.log.error('---------参数校验有误---------')

        self.log.info('---------step2:显示版本号信息;except:显示正确------------')
        cmd_list = ['-V', '--version']
        for cmd in cmd_list:
            check_cmd1 = f'''source {macro.DB_ENV_PATH}
                                gs_checkperf {cmd}'''
            self.log.info(check_cmd1)
            msg1 = self.primary_node.sh(check_cmd1).result()
            self.log.info(msg1)
            self.assertIn('openGauss', msg1)

        text = '----step3:opengauss安装安装用户检查openGauss性能;expect:正常显示检查结果----'
        self.log.info(text)
        checkperf_cmd = f'source {macro.DB_ENV_PATH};' \
                        f'gs_checkperf ' \
                        f'-U {self.primary_node.ssh_user};'
        self.log.info(checkperf_cmd)
        checkperf_result = self.primary_node.sh(checkperf_cmd).result()
        self.log.info(checkperf_result)
        result_list1 = checkperf_result.splitlines()
        result_list = []
        for i in result_list1:
            if ':' in i:
                result_list.append(i.split(':')[0].strip())
        self.log.info(result_list)
        checkperf_result = ['Cluster statistics information',
                            'Host CPU busy time ratio',
                            'MPPDB CPU time % in busy time',
                            'Shared Buffer Hit ratio',
                            'In-memory sort ratio',
                            'Physical Reads',
                            'Physical Writes',
                            'DB size',
                            'Total Physical writes',
                            'Active SQL count',
                            'Session count']
        self.assertEqual(checkperf_result.sort(), result_list.sort(),
                         '执行失败:' + text)

    def tearDown(self):
        self.log.info('--------------无需清理环境-------------------')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')